import Mock from 'mockjs';
// 生成Base64图片
Mock.Random.base64Image = (width = 150, height = 150) => {
  return `${width}AA${height}AAAALCAYAAAC${Mock.Random.word(5)}AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAALIKSURBVHhe7Z0JeBtFFIV/3z78+8${Mock.Random.word(100)}=`;
};

// 模拟医生数据
const doctorList = Mock.mock({
  'data|20': [
    {
      id: '@integer(1, 100)',
      doctorName: '@cname',
      photo: '@base64Image()',
      hospitalArea: '@pick(["east", "west", "south", "north", "central"])',
      department: '@pick(["department1", "department2", "department3", "department4"])',
      position: '@pick(["主任医师", "副主任医师", "主治医师", "医师"])',
      registrationFee: '@float(10, 200, 2, 2)',
      introduction: '@cparagraph(1, 3)',
      status: '@boolean'
    }
  ]
}).data;

// 模拟API接口
Mock.mock('/api/doctors', 'get', (config: any) => {
  // const params = new URLSearchParams(config.url.split('?')[1]);
  // const page = Number(params.get('page')) || 1;
  // const size = Number(params.get('size')) || 5;
  // const department = params.get('department') || '';
  // const name = params.get('name') || '';
  
  // // 过滤数据
  // let filteredDoctors = [...doctorList];
  
  // if (department) {
  //   filteredDoctors = filteredDoctors.filter(doctor => 
  //     doctor.department === department
  //   );
  // }
  
  // if (name) {
  //   filteredDoctors = filteredDoctors.filter(doctor => 
  //     doctor.doctorName.includes(name)
  //   );
  // }
  
  // const total = filteredDoctors.length;
  // const start = Math.min((page - 1) * size, filteredDoctors.length);
  // const end = Math.min(page * size, filteredDoctors.length);

  return {
    code: 200,
    message: 'success',
    data: {
      data: doctorList
    }
  };
});


Mock.mock(/\/api\/doctors\/\d+/, 'get', (config: any) => {
  const id = Number(config.url.split('/').pop());
  const doctor = doctorList.find(item => item.id === id);
  return {
    code: 200,
    message: 'success',
    data: doctor
  };
});

Mock.mock('/api/doctors', 'post', (config: any) => {
  const data = JSON.parse(config.body);
  const newDoctor = { ...data, id: Date.now() };
  doctorList.push(newDoctor);
  return {
    code: 200,
    message: '新增成功',
    data: newDoctor
  };
});

Mock.mock(/\/api\/doctors\/\d+/, 'put', (config: any) => {
  const id = Number(config.url.split('/').pop());
  const data = JSON.parse(config.body);
  const index = doctorList.findIndex(item => item.id === id);
  
  if (index!== -1) {
    doctorList[index] = { ...doctorList[index], ...data };
  }
  
  return {
    code: 200,
    message: '更新成功',
    data: doctorList[index]
  };
});

Mock.mock(/\/api\/doctors\/\d+/, 'delete', (config: any) => {
  const id = Number(config.url.split('/').pop());
  const index = doctorList.findIndex(item => item.id === id);
  
  if (index!== -1) {
    doctorList.splice(index, 1);
  }
  
  return {
    code: 200,
    message: '删除成功'
  };
});